const { mongodb, ObjectId } = require("../config/mongodb");
const collectionName = "users"; //users表
class UsersModel {
  constructor() {}
  findById(id) {
    return mongodb.findById(collectionName, id);
  }
  login(body) {
    return new Promise((resolve, reject) => {
      //回调函数式获取数据
      mongodb.getCollection(collectionName, async (err, collection, client) => {
        if (err) reject(err.message); //这是回调传过来的可能出现的连接错误
        try {
          const res = await collection.find(body).toArray();
          resolve(res);
        } catch (err) {
          reject(err.message); //这是执行操作语句可能出现的错误
        } finally {
          client.close(); //操作完,最后关闭连接
        }
      });
    });
  }
  login1(body) {
    return new Promise((resolve, reject) => {
      //回调 通过then  获取数据
      mongodb
        .getPromiseCollection(collectionName)
        .then(async (array) => {
          const [collection, client] = array;
          try {
            const res = await collection.find(body).toArray();
            resolve(res);
          } catch (err) {
            reject(err.message); //这是执行操作语句可能出现的错误
          } finally {
            client.close(); //操作完,最后关闭连接
          }
        })
        .catch((err) => reject(err.message)); //这是可能出现的连接错误
    });
  }
  findByUsername(username) {
    return new Promise((resolve, reject) => {
      mongodb.getCollection(collectionName, async (err, collection, client) => {
        if (err) reject(err.message);
        try {
          const res = await collection.find({ username: username }).toArray();
          resolve(res);
        } catch (err) {
          reject(err.message);
        } finally {
          client.close();
        }
      });
    });
  }
  register(body) {
    return new Promise((resolve, reject) => {
      mongodb.getCollection(collectionName, async (err, collection, client) => {
        if (err) reject(err.message);
        try {
          const res = await collection.insertOne(body);
          resolve(res);
        } catch (err) {
          reject(err.message);
        } finally {
          client.close();
        }
      });
    });
  }
  updateById(id, body) {
    const { username, password } = body; //从body 中解构出来用户名和密码
    return new Promise((resolve, reject) => {
      mongodb.getCollection(collectionName, async (err, collection, client) => {
        if (err) reject(err.message);
        try {
          const res = await collection.updateOne(
            { _id: ObjectId(id) }, //修改条件
            { $set: { username: username, password: password } } //修改的数据
          );
          resolve(res);
        } catch (err) {
          reject(err.message);
        } finally {
          client.close();
        }
      });
    });
  }
  deleteById(id) {
    return new Promise((resolve, reject) => {
      mongodb.getCollection(collectionName, async (err, collection, client) => {
        if (err) reject(err.message);
        try {
          const res = await collection.deleteOne({ _id: ObjectId(id) });
          resolve(res);
        } catch (err) {
          reject(err.message);
        } finally {
          client.close();
        }
      });
    });
  }
}

module.exports = new UsersModel();
